package com.fanghaoran.notes.controller;

import com.alibaba.fastjson.JSON;
import com.fanghaoran.notes.bean.Page;
import com.fanghaoran.notes.entity.Product;
import com.fanghaoran.notes.entity.User;
import com.fanghaoran.notes.service.ProductService;
import com.fanghaoran.notes.service.impl.ProductServiceImpl;
import com.fanghaoran.notes.util.CodeUtil;
import com.fanghaoran.notes.util.Constants;
import com.fanghaoran.notes.util.StringUtil;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;


import java.io.*;
import java.math.BigDecimal;
import java.sql.SQLException;
import java.text.ParseException;

/**
 * @Author: win11
 * @Date: 2023/04/18/22:25
 * @Description: 商品模块表现层
 */
@WebServlet("/product")
public class ProductController extends BaseServlet{
    /**
     * service对象
     */
    ProductService productService=new ProductServiceImpl();

    /**
     * 页面展示商品数据
     * @param request 请求
     * @param response 响应
     * @throws Exception 异常
     */
    public void show(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //接收请求参数 ->商品类别id 当前页数 设定好每页条数
        String tId = request.getParameter("tId");
        //查询的商品名
        String pName=request.getParameter("pName");
        //查询的排序方式(订单数/评分)
        String orderByStr = request.getParameter("orderBy");
        //获取当前页数
        String currentPage = request.getParameter("currentPage");
        //每页条数
        int pageSize=12;
        //没选默认是1
        int page=1;
        if(currentPage!=null){
            page=Integer.parseInt(currentPage);
        }
        int orderBy=1;
        if(orderByStr!=null){
            orderBy=Integer.parseInt(orderByStr);
        }
        //业务处理并返回前端需要的数据(商品信息 总页数等)
        Page<Product> productPage=null;
        if(pName!=null){
            productPage = (Page<Product>) productService.getProductPageByName(page, pageSize, pName,orderBy).getData();
        }
        else if(tId!=null) {
            productPage = (Page<Product>) productService.getProductPageByTid(page, pageSize, Integer.parseInt(tId),orderBy).getData();
        }
        //响应
        response.getWriter().println(JSON.toJSONString(productPage));
    }

    /**
     * 展示商品详情
     * @param request 请求
     * @param response 响应
     * @throws Exception 异常
     */
    public void detail(HttpServletRequest request,HttpServletResponse response) throws Exception {
        //获取商品id
        String pId=request.getParameter("pId");
        //业务层查询并响应
        response.getWriter().println(JSON.toJSONString(productService.getProductByPid(Integer.parseInt(pId))));
        //System.out.println(JSON.toJSONString(product));
    }

    /**
     * 设为收藏
     * @param request 请求
     * @param response 响应
     * @throws SQLException 异常
     * @throws IOException 异常
     */
    public void setAsFavorite(HttpServletRequest request,HttpServletResponse response) throws SQLException, IOException {
        //获取前端参数
        String pId = request.getParameter("pId");
        String isFavorite = request.getParameter("isFavorite");
        //获取用户
        User user=(User)request.getSession().getAttribute(Constants.LOGIN_SESSION_PARAM);
        Integer uId = user.getUId();
        //传给业务层
        response.getWriter().println(JSON.toJSONString(productService.setAsFavorite(uId,Integer.parseInt(pId),Integer.parseInt(isFavorite))));
    }

    /**
     * 某个用户是否把某件商品作为收藏
     * @param request 请求
     * @param response 响应
     * @throws Exception 异常
     */
    public void getFavoriteByUidAndPid(HttpServletRequest request,HttpServletResponse response) throws Exception {
        //获取前端参数
        String pId = request.getParameter("pId");
        System.out.println(request.getSession().getAttribute(Constants.LOGIN_SESSION_PARAM));
        //获取用户
        User user=(User)request.getSession().getAttribute(Constants.LOGIN_SESSION_PARAM);
        if(user!=null) {
            Integer uId = user.getUId();
            System.out.println(JSON.toJSONString(productService.getFavoriteByUidAndPid(uId,Integer.parseInt(pId))));
            //传给业务层
            response.getWriter().println(JSON.toJSONString(productService.getFavoriteByUidAndPid(uId,Integer.parseInt(pId))));
        }
    }

    /**
     * 添加商品
     * @param request 请求
     * @param response 响应
     * @throws Exception 异常
     */
    public void addProduct(HttpServletRequest request,HttpServletResponse response) throws Exception {
        //接收前端数据
        String productName = request.getParameter("name");
        System.out.println(productName);
        String productPrice = request.getParameter("price");
        BigDecimal price = new BigDecimal(productPrice);
        String productStock = request.getParameter("stock");
        String productInfo = request.getParameter("info");
        String productType = request.getParameter("type");
        String productFile = request.getParameter("file");
        String fileName= StringUtil.getFileName(productFile);

        //获取商家
        User user=(User)request.getSession().getAttribute(Constants.LOGIN_SESSION_PARAM);
        if(user!=null) {
            Integer uId = user.getUId();
            //传给业务层
            response.getWriter().println(
                    JSON.toJSONString(
                            productService.addProduct(uId,productName,price,Integer.parseInt(productStock),productInfo,Integer.parseInt(productType),fileName)
                    )
            );
        }
    }

    /**
     * 通过商家id获取属于该商家的商品信息
     * @param request 请求
     * @param response 响应
     * @throws Exception 异常
     */
    public void getProductByUid(HttpServletRequest request,HttpServletResponse response) throws Exception {
        //获取商家id
        User user=(User)request.getSession().getAttribute(Constants.LOGIN_SESSION_PARAM);
        if(user!=null) {
            Integer uId = user.getUId();
            //传给业务层
            response.getWriter().println(JSON.toJSONString(productService.getProductByUid(uId)));
        }
    }

    /**
     * 修改商品
     * @param request 请求
     * @param response 响应
     * @throws SQLException 异常
     * @throws IOException 异常
     */
    public void modifyProduct(HttpServletRequest request,HttpServletResponse response) throws SQLException, IOException {
        //接收前端参数
        String pId = request.getParameter("pId");
        String pName = request.getParameter("pName");
        String price = request.getParameter("price");
        String stock = request.getParameter("stock");
        String pInfo = request.getParameter("pInfo");
        //根据pid业务层处理修改操作
        response.getWriter().println(JSON.toJSONString(
                productService.modifyProduct(Integer.parseInt(pId),pName,new BigDecimal(price),Integer.parseInt(stock),pInfo)
        ));
    }

    /**
     * 获取所有商品的排名
     * @param request 请求
     * @param response 响应
     * @throws Exception 异常
     */
    public void getProductRank(HttpServletRequest request,HttpServletResponse response) throws Exception {
        //业务层获取数据
        response.getWriter().println(JSON.toJSONString(productService.getProductRank()));
    }

    /**
     * 发送优惠券
     * @param request 请求
     * @param response 响应
     * @throws Exception 异常
     */
    public void sendDiscount(HttpServletRequest request,HttpServletResponse response) throws Exception {
        //获取前端参数
        int pId = Integer.parseInt(request.getParameter("pId"));
        int type = Integer.parseInt(request.getParameter("type"));
        int amount = Integer.parseInt(request.getParameter("amount"));
        //业务层插入
        response.getWriter().println(JSON.toJSONString(productService.sendDiscount(pId,type,amount)));
    }

    /**
     * 展示又回去
     * @param request 请求
     * @param response 响应
     * @throws Exception 异常
     */
    public void showDiscount(HttpServletRequest request,HttpServletResponse response) throws Exception {
        //获取前端参数
        int pId = Integer.parseInt(request.getParameter("pId"));
        //业务层处理
        response.getWriter().println(JSON.toJSONString(productService.showDiscount(pId)));
    }

    /**
     * 修改优惠券数量
     * @param request 请求
     * @param response 响应
     * @throws Exception 异常
     */
    public void modifyDiscount(HttpServletRequest request,HttpServletResponse response) throws Exception {
        //前端参数
        int pId = Integer.parseInt(request.getParameter("pId"));
        int tId = Integer.parseInt(request.getParameter("tId"));
        int before = Integer.parseInt(request.getParameter("dAmount"));
            //修改的变化量
        int change = Integer.parseInt(request.getParameter("change"));
        //修改数量
        int after=before+change;
        //业务层修改
        response.getWriter().println(JSON.toJSONString(productService.modifyDiscount(pId,tId,after)));
    }

    /**
     * 展示某个用户优惠券的使用情况
     * @param request 请求
     * @param response 响应
     * @throws Exception 异常
     */
    public void showUsageOfDiscount(HttpServletRequest request,HttpServletResponse response) throws Exception {
        //获取前端参数
        int pId = Integer.parseInt(request.getParameter("pId"));
        //获取用户
        User user=(User)request.getSession().getAttribute(Constants.LOGIN_SESSION_PARAM);
        Integer uId = user.getUId();
        //业务层处理
        response.getWriter().println(JSON.toJSONString(productService.showUsageOfDiscount(pId,uId)));
    }

    /**
     * 添加优惠券的使用情况(状态 用户第一次领取优惠券时)
     * @param request 请求
     * @param response 响应
     * @throws SQLException 异常
     * @throws IOException 异常
     */
    public void addUsageOfDiscount(HttpServletRequest request,HttpServletResponse response) throws SQLException, IOException {
        //获取前端参数
        int pId = Integer.parseInt(request.getParameter("pId"));
        int tId = Integer.parseInt(request.getParameter("tId"));
        int dAmount = Integer.parseInt(request.getParameter("dAmount"));
        int dState = Integer.parseInt(request.getParameter("dState"));
        //获取用户
        User user=(User)request.getSession().getAttribute(Constants.LOGIN_SESSION_PARAM);
        Integer uId = user.getUId();
        //业务层插入
        response.getWriter().println(JSON.toJSONString(productService.addUsageOfDiscount(pId,uId,tId,dAmount,dState)));
    }

    /**
     * 展示优惠券使用情况详情
     * @param request 请求
     * @param response 响应
     * @throws Exception 异常
     */
    public void showDiscountUsageDetail(HttpServletRequest request,HttpServletResponse response) throws Exception {
        //获取前端参数
        int pId = Integer.parseInt(request.getParameter("pId"));
        //获取用户
        User user=(User)request.getSession().getAttribute(Constants.LOGIN_SESSION_PARAM);
        Integer uId = user.getUId();
        //业务层
        response.getWriter().println(JSON.toJSONString(productService.showDiscountUsageDetail(pId,uId)));
    }

    /**
     * 修改优惠券的使用情况
     * @param request 请求
     * @param response 响应
     * @throws SQLException 异常
     * @throws IOException 异常
     */
    public void modifyUsageOfDiscount(HttpServletRequest request,HttpServletResponse response) throws SQLException, IOException {
        //获取前端参数
        int pId = Integer.parseInt(request.getParameter("pId"));
        int state = Integer.parseInt(request.getParameter("state"));
        //获取用户
        User user=(User)request.getSession().getAttribute(Constants.LOGIN_SESSION_PARAM);
        Integer uId = user.getUId();
        //业务层修改
        response.getWriter().println(JSON.toJSONString(productService.modifyUsageOfDiscount(pId,uId,state)));
    }

    /**
     * 定时修改商品的库存和价格
     * @param request 请求
     * @param response 响应
     * @throws ParseException 异常
     * @throws IOException 异常
     */
    public void modifyProductInTime(HttpServletRequest request,HttpServletResponse response) throws ParseException, IOException {
        //获取前端参数
        int pId = Integer.parseInt(request.getParameter("pId"));
        BigDecimal price = new BigDecimal(request.getParameter("price"));
        int stock = Integer.parseInt(request.getParameter("stock"));
        String scheduledTime = request.getParameter("scheduledTime");
        //业务层处理
        response.getWriter().println(JSON.toJSONString(productService.modifyProductInTime(pId,price,stock,scheduledTime)));
    }

    /**
     * 展示/获取邀请码相关信息
     * @param request 请求
     * @param response 响应
     * @throws Exception 异常
     */
    public void showInviteDetail(HttpServletRequest request,HttpServletResponse response) throws Exception {
        //获取前端参数
        int pId = Integer.parseInt(request.getParameter("pId"));
        //获取用户
        User user=(User)request.getSession().getAttribute(Constants.LOGIN_SESSION_PARAM);
        Integer uId = user.getUId();
        //业务层查询
        response.getWriter().println(JSON.toJSONString(productService.showInviteDetail(pId,uId)));
    }

    /**
     * 检查邀请码是否在有效时间内
     * @param request 请求
     * @param response 响应
     * @throws Exception 异常
     */
    public void isInviteInValidTime(HttpServletRequest request,HttpServletResponse response) throws Exception {
        //获取前端参数
        String inviteCode = request.getParameter("inviteCode");
        //业务层逻辑处理
        response.getWriter().println(JSON.toJSONString(productService.isInviteInValidTime(inviteCode)));
    }

    /**
     * 生成邀请码
     * @param request 请求
     * @param response 响应
     * @throws IOException 异常
     */
    public void getInviteCode(HttpServletRequest request,HttpServletResponse response) throws IOException {
        response.getWriter().println(CodeUtil.generateInviteCode());
    }

    /**
     * 添加/插入邀请信息
     * @param request 请求
     * @param response 响应
     * @throws SQLException 异常
     * @throws IOException 异常
     */
    public void addInviteDetail(HttpServletRequest request,HttpServletResponse response) throws SQLException, IOException {
        //前端参数
        int pId = Integer.parseInt(request.getParameter("pId"));
        BigDecimal refundCount=new BigDecimal(request.getParameter("refundCount"));
        String inviteCode = request.getParameter("inviteCode");
        //获取用户
        User user=(User)request.getSession().getAttribute(Constants.LOGIN_SESSION_PARAM);
        Integer uId = user.getUId();
        //业务层处理
        response.getWriter().println(JSON.toJSONString(productService.addInviteDetail(pId,refundCount,inviteCode,uId)));
    }

    /**
     * 修改邀请码状态
     * @param request 请求
     * @param response 响应
     * @throws SQLException 异常
     * @throws IOException 异常
     */
    public void modifyInviteState(HttpServletRequest request,HttpServletResponse response) throws SQLException, IOException {
        //获取前端参数
        int pId = Integer.parseInt(request.getParameter("pId"));
        int codeState = Integer.parseInt(request.getParameter("codeState"));
        //获取用户
        User user=(User)request.getSession().getAttribute(Constants.LOGIN_SESSION_PARAM);
        Integer uId = user.getUId();
        //业务层修改
        response.getWriter().println(JSON.toJSONString(productService.modifyInviteState(pId,uId,codeState)));
    }

    /**
     * 检查邀请码的有效性
     * @param request 请求
     * @param response 响应
     * @throws Exception 异常
     */
    public void checkQualification(HttpServletRequest request,HttpServletResponse response) throws Exception {
        //获取前端参数
        String inviteCode = request.getParameter("inviteCode");
        //获取用户
        User user=(User)request.getSession().getAttribute(Constants.LOGIN_SESSION_PARAM);
        Integer uId = user.getUId();
        //业务层检验
        response.getWriter().println(JSON.toJSONString(productService.checkQualification(uId,inviteCode)));
    }
}
